package com.azxcrm.project.system.customer.service;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.azxcrm.common.constant.CommonConstants;
import com.azxcrm.common.utils.ContrastUtils;
import com.azxcrm.common.utils.StringUtils;
import com.azxcrm.common.utils.security.ShiroUtils;
import com.azxcrm.project.system.basic.domain.CustomerModifyRecord;
import com.azxcrm.project.system.basic.service.ICustomerModifyRecordService;
import com.azxcrm.project.system.customer.dao.ICustomerInfoDao;
import com.azxcrm.project.system.customer.domain.CustomerClue;
import com.azxcrm.project.system.customer.domain.CustomerInfo;
import com.azxcrm.project.system.customer.service.ICustomerInfoService;

/**
 * 客户 服务层实现
 * 
 * @author Dustyone
 * @date 2018-09-04
 */
@Service
public class CustomerInfoServiceImpl implements ICustomerInfoService {
	@Autowired
	private ICustomerInfoDao customerInfoDao;

	@Autowired
	private ICustomerModifyRecordService customerModifyRecordService;

	/**
	 * 查询客户信息
	 * 
	 * @param customerId
	 *            客户ID
	 * @return 客户信息
	 */
	@Override
	public CustomerInfo selectCustomerInfoById(Integer customerId) {
		return customerInfoDao.selectCustomerInfoById(customerId);
	}

	/**
	 * 查询客户背景类型
	 */
	@Override
	public List<CustomerInfo> selectCustomerBackgroundId(Integer backgroundId) {
		return customerInfoDao.selectCustomerBackgroundId(backgroundId);
	}

	/**
	 * 通过手机号查询信息
	 * 
	 * @param cellphone
	 * 
	 * @return 客户信息
	 */
	@Override
	public CustomerInfo selectCustomerCellPhone(String cellphone) {
		return customerInfoDao.selectCustomerCellPhone(cellphone);
	}

	/**
	 * 根据身份证号码查询客户信息
	 */
	@Override
	public CustomerInfo selectCustomerByIdNo(String identityNum) {
		return customerInfoDao.selectCustomerByIdNo(identityNum);
	}
	
	/**
	 * 查询客户列表
	 * 
	 * @param customerInfo
	 *            客户信息
	 * @return 客户集合
	 */
	@Override
	public List<CustomerInfo> selectCustomerInfoList(CustomerInfo customerInfo) {
		return customerInfoDao.selectCustomerInfoList(customerInfo);
	}

	/**
	 * 新增客户
	 * 
	 * @param customerInfo
	 *            客户信息
	 * @return 结果
	 */
	@Override
	public int insertCustomerInfo(CustomerInfo customerInfo) {
		return customerInfoDao.insertCustomerInfo(customerInfo);
	}

	/**
	 * 修改客户
	 * 
	 * @param customerInfo
	 *            客户信息
	 * @return 结果
	 */
	@Override
	public int updateCustomerInfo(CustomerInfo customerInfo) {
		return customerInfoDao.updateCustomerInfo(customerInfo);
	}

	/**
	 * 保存客户
	 * 
	 * @param customerInfo
	 *            客户信息
	 * @return 结果
	 */
	@Override
	public int saveCustomerInfo(CustomerInfo customerInfo) {
		Integer customerId = customerInfo.getCustomerId();
		CustomerModifyRecord customerModifyRecord = new CustomerModifyRecord();
		int rows = 0;
		if (StringUtils.isNotNull(customerId)) {
			CustomerInfo cus = customerInfoDao.selectCustomerInfoById(customerInfo.getCustomerId());
			String change = ContrastUtils.contrastObj(cus, customerInfo);
			customerModifyRecord.setCustomerId(customerInfo.getCustomerId());
			customerModifyRecord.setModifyBy(ShiroUtils.getUserId().intValue());
			customerModifyRecord.setModifyDetail(change);
			customerModifyRecordService.saveCustomerModifyRecord(customerModifyRecord);
			rows = customerInfoDao.updateCustomerInfo(customerInfo);
		} else {
			customerInfo.setCreateBy(ShiroUtils.getLoginName());
			rows = customerInfoDao.insertCustomerInfo(customerInfo);
		}
		return rows;
	}

	/**
	 * 删除客户信息
	 * 
	 * @param customerId
	 *            客户ID
	 * @return 结果
	 */
	@Override
	public int deleteCustomerInfoById(Integer customerId) {
		return customerInfoDao.deleteCustomerInfoById(customerId);
	}

	/**
	 * 批量删除客户对象
	 * 
	 * @param customerIds
	 *            需要删除的数据ID
	 * @return 结果
	 */
	@Override
	public int batchDeleteCustomerInfo(Integer[] customerIds) {
		return customerInfoDao.batchDeleteCustomerInfo(customerIds);
	}

	/**
	 * 校验客户手机是否重复
	 * 
	 * @param cellphone
	 *            需要查询的手机号
	 * @return
	 */
	@Override
	public String checkCellphone(String cellphone) {
		int count = customerInfoDao.checkCellphone(cellphone);
		if (count > 0) {
			return CommonConstants.FAIL;
		}
		return CommonConstants.SUCCESS;
	}
	
	/**
	 * 客户信息转化封装方法
	 * @param customerClue
	 * @param customerInfo
	 * @return
	 */
	@Override
	public CustomerInfo CustomerConvert(CustomerClue customerClue){
		CustomerInfo customerInfo = new CustomerInfo();
		try {
			//客户名
			if(customerClue.getCustomerName() != null){
				customerInfo.setName(customerClue.getCustomerName());
			}
			//手机号码
			if(customerClue.getCustomerCellphone() != null){
				customerInfo.setCellphone(customerClue.getCustomerCellphone());
			}
			//联系好吗
			if(customerClue.getCustomerPhone() != null){
				customerInfo.setPhoneNum(customerClue.getCustomerPhone());
			}
			
			//年龄
			if(customerClue.getCustomerAge() != null){
				customerInfo.setAge(customerClue.getCustomerAge());
			}
			//学历
			if(customerClue.getCustomerEducation() != null){
				customerInfo.setEducation(customerClue.getCustomerEducation());
			}
			//性别
			if(customerClue.getCustomerSex() != null){
				customerInfo.setSex(customerClue.getCustomerSex());
			}
			//身份证号码
			if(customerClue.getCustomerIdentityNum() != null){
				customerInfo.setIdentityNum(customerClue.getCustomerIdentityNum());
			}
			//生日
			if(customerClue.getCustomerBirth() != null){
				customerInfo.setBirth(customerClue.getCustomerBirth());
			}
			//职业
			if(customerClue.getCustomerJob() != null){
				customerInfo.setJob(customerClue.getCustomerJob());
			}
			//省份
			if(customerClue.getCustomerProvince() != null){
				customerInfo.setProvince(customerClue.getCustomerProvince());
			}
			//城市
			if(customerClue.getCustomerCity() != null){
				customerInfo.setCity(customerClue.getCustomerCity());
			}
			//市区
			if(customerClue.getCustomerArea() != null){
				customerInfo.setArea(customerClue.getCustomerArea());
			}
			//地址详情
			if(customerClue.getCustomerAddressDetail() != null){
				customerInfo.setAddressDetail(customerClue.getCustomerAddressDetail());
			}
			
			//创建者
			if(customerClue.getCreateBy() !=null){
				customerInfo.setCreateBy(customerClue.getCreateBy());
			}
		} catch (Exception e) {
		}
		return customerInfo;
	}
	
	/**
	 * 模糊查询客户信息
	 * 
	 * @param name 需要查询的用户名，@param cellphone 需要查询的手机号，@param identityNum 需要查询的身份证号
	 * @return 结果
	 */
	@Override
	public List<CustomerInfo> selectCustomerInfo(CustomerInfo customerInfo) {
		// TODO Auto-generated method stub
		return customerInfoDao.selectCustomerInfo(customerInfo);
	}
}
